<?php
namespace app\serverfund\service\fund;

use \app\manage\service\ApiBase;
/**
 * 用户的资金账户
 * @author adv30 <adv30@126.com> 2017年6月19日10:33:26
 */
class FundAccount extends ApiBase
{
    protected $fundAccountValidate = '\app\serverfund\validate\FundAccount';
    protected $fundAccountLogic = '\app\serverfund\logic\FundAccount';

    //封装一层检验user_info、opposite_info后再执行stepRun <该页多数方法都需检验这两个json数据>
    protected function _checkJsonThenRun(array $data, string $method, string $scene = ''): array
    {
        if(isset($data['user_info']) && $data['user_info'] !== '{}'){ $data['user_info'] = json_encode($data['user_info']); }
        if(isset($data['opposite_info']) && $data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }
        if(isset($data['total_other_price']) && $data['total_other_price'] !== '{}'){ $data['total_other_price'] = json_encode($data['total_other_price']); }
        if(isset($data['buyer_info']) && $data['buyer_info'] !== '{}'){ $data['buyer_info'] = json_encode($data['buyer_info']); }
        if(isset($data['seller_info']) && $data['seller_info'] !== '{}'){ $data['seller_info'] = json_encode($data['seller_info']); }
        if(isset($data['system_info']) && $data['system_info'] !== '{}'){ $data['system_info'] = json_encode($data['system_info']); }

        return $this->stepRun($data, $method, $scene);
    }

   /*兑换钱*/
    public function exchange(): array
    {
        $data = $this->haveIssetReceiveData(['exchange'=>' ', 'money'=>0, 'app_id'=>0, 'currency_scale'=>'']);

        $validate = validate($this->fundAccountValidate);
        if(!$validate->scene('exchange')->check($data)){
            return verify_err(null, $validate->getError());
        }

        $classPath = '\app\serverfund\logic' . '\\' . $data['exchange'] . 'Account';
        $Obj = new $classPath($data);
        return $this->logic($this->fundAccountLogic)->exchange($Obj, $data);
    }

    // 初始化一个资金账户,如果帐户存在,就清0,如果不存在,就创建一个
    public function init(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'create_ip'=>' ', 'app_id'=>' ']);
        return $this->stepRun($data, 'init', 'init');
    }
    # 充值 系统->a
    public function prepaid(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>' ', 'user_info'=>'{}','opposite_info'=>'{}', 'remark'=>'']);
        if(\SYSTEM_ID == $data['user_id']){
            return \verify_err($data, '不能为系统用户充值');
        }
        return $this->_checkJsonThenRun($data, 'prepaid', 'prepaid');
    }
    # 转账 a->b
    public function transfer(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'transfer', 'transfer');
    }
    # 付款 a->b 暂时废弃,用转账功能完成
    public function payment(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0']);
        return $this->stepRun($data, 'payment', 'payment');
    }
    # 借款 a借b的
    public function borrow(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'borrow', 'borrow');
    }
    # 还款 a还b的
    public function refund(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'borrow_log_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'refund', 'refund');
    }
    /*唯一索引：`user_id`,`app_id`,`type`,`obj_id`,`freeze_counts`,`freeze_status`*/
    # 冻结 a
    public function freeze(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','type'=>' ','obj_id'=>' ','freeze_counts'=>1]);
        return $this->_checkJsonThenRun($data, 'freeze', 'freeze');
    }
    /*管理员冻结某用户资金*/
    public function adminFreeze(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','reason'=>' ']);
        return $this->_checkJsonThenRun($data, 'adminFreeze', '');
    }
    # 解冻 a
    public function unfreeze(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'money'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','type'=>' ','obj_id'=>' ','freeze_counts'=>' ','remark'=>'']);
        return $this->_checkJsonThenRun($data, 'unfreeze', 'unfreeze');
    }
    # 红包功能 a->b b(enter)
    // 获取列表
    public function list(): array
    {
        $data = $this->haveIssetReceiveData(['page'=>'1', 'list_rows'=>'', 'keywords'=>'']);
        return $this->stepRun($data, 'list', 'list');
    }
    // 某user_ids的用户资金列表
    public function listByUserIds(): array
    {
        $data = $this->haveIssetReceiveData(['user_ids'=>' ']);
        return $this->stepRun($data, 'listByUserIds', 'listByUserIds');
    }

    /*获取某用户的所有资产余额*/
    public function userFund(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0]);
        return $this->stepRun($data, 'userFund', 'userFund');
    }

    /*余额付款的订单进行确认收货操作 opposite_id解冻user_id user_id转账opposite_id*/
    //解冻用户资金 用户转账平台资金
    public function affirmPickUpGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'certificate_price'=>' ', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}', 'opposite_info'=>'{}', 'type'=>' ', 'order_id'=>' ', 'pay_method'=>' ']);
        return $this->_checkJsonThenRun($data, 'affirmPickUpGoods');
    }

    /*我的证书 - 退款*/
    public function sendBack(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'order_id'=>0, 'certificate_price'=>' ', 'pay_method'=>' ', 'certi_transfer_fee'=>0, 'earnings'=>0, 'update_ip'=>' ', 'app_id'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'sendBack');
    }

    // 同意证书转让或购买证书时的资金操作
    // procedure_cost 手续费
    // sell_price 证书的销售价格
    // buyer_id 买家
    // seller_id 卖家
    // 验证了抵扣积分信息是否正确
    public function buyCertificate(): array 
    {
        $data = $this->haveIssetReceiveData(['certificate_id'=>0, 'buyer_id'=>0, 'seller_id'=>0, 'system_id'=>0, 'sell_price'=>0,'procedure_cost'=>0,'update_ip'=>'','app_id'=>0,'buyer_info'=>'{}','seller_info'=>'{}','system_info'=>'{}','is_use_integral'=>0,'certi_max_integral_deduction'=>0,'integral_deduction'=>0,'integral_deduction_fund'=>0]);
        return $this->_checkJsonThenRun($data, 'buyCertificate', 'buyCertificate');
    }
    // 证书改价时资金的操作
    public function certiChangePrice(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'old_procedure_price'=>0, 'new_procedure_price'=>0, 'update_ip'=>'', 'app_id'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}', 'certificate_id'=>0]);
        return $this->_checkJsonThenRun($data, 'certiChangePrice');
    }
    // 解冻用户改价时的手续费(拒绝定向单后，还原证书设置时进行的操作)
    public function unfreezeProcedure(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'opposite_id'=>0, 'procedure_price'=>0, 'user_info'=>'{}', 'opposite_info'=>'{}', 'certificate_id'=>0]);
        return $this->_checkJsonThenRun($data, 'unfreezeProcedure');
    }

}